<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1>工作时间记录系统</h1>
        <p>请输入您的口令登录</p>
      </div>
      
      <el-form 
        :model="loginForm" 
        :rules="rules" 
        ref="loginFormRef"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="token">
          <el-input
            v-model="loginForm.token"
            placeholder="请输入口令（数字+字母，最多20位）"
            size="large"
            :maxlength="20"
            show-word-limit
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Key /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            size="large" 
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-tips">
        <p>💡 提示：</p>
        <ul>
          <li>口令只能包含数字和字母</li>
          <li>如果口令不存在，系统会自动为您创建账户</li>
          <li>请妥善保管您的口令</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAppStore } from '../stores/index.js'

const router = useRouter()
const store = useAppStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  token: ''
})

const rules = {
  token: [
    { required: true, message: '请输入口令', trigger: 'blur' },
    { min: 1, max: 20, message: '口令长度不能超过20位', trigger: 'blur' },
    { 
      pattern: /^[a-zA-Z0-9]+$/, 
      message: '口令只能包含数字和字母', 
      trigger: 'blur' 
    }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const response = await store.login(loginForm.token)
    
    if (response.code === 200) {
      ElMessage.success(response.message)
      
      // 加载用户状态
      await store.getUserStatus()
      
      // 跳转到主页面
      router.push('/dashboard')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 检查是否已登录（不验证token，避免重复验证）
onMounted(() => {
  // 如果本地有token，直接跳转到Dashboard
  // Dashboard会负责验证token有效性
  if (store.checkLoginStatus()) {
    router.push('/dashboard')
  }
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #333;
  margin-bottom: 10px;
  font-size: 24px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-tips {
  margin-top: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
  font-size: 13px;
  color: #666;
}

.login-tips p {
  margin-bottom: 8px;
  font-weight: 500;
}

.login-tips ul {
  margin: 0;
  padding-left: 20px;
}

.login-tips li {
  margin-bottom: 4px;
}
</style>
